<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="活动标题" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动关键词" prop="activityKey">
        <el-input
          v-model="queryParams.activityKey"
          placeholder="请输入活动关键词"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动图片地址" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入活动图片地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="界面编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入界面编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动参数" prop="arg">
        <el-input
          v-model="queryParams.arg"
          placeholder="请输入活动参数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跳转类型" prop="isReturn">
        <el-input
          v-model="queryParams.isReturn"
          placeholder="请输入跳转类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="ordersType">
        <el-select v-model="queryParams.ordersType" placeholder="请选择订单类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否开启" prop="isActivity">
        <el-input
          v-model="queryParams.isActivity"
          placeholder="请输入是否开启"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.beginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.endTime"
          type="date"
          value-format="HH-mm-ss"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sysActivity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sysActivity:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sysActivity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sysActivity:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sysActivityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="活动标题" align="center" prop="name" />
      <el-table-column label="活动关键词" align="center" prop="activityKey" />
      <el-table-column label="活动图片地址" align="center" prop="url" >
        <template slot-scope="{row}">
          <el-image
            class="image"
            :src="row.url"
            :preview-src-list="[row.url]"
            fit="contain"
            lazy
          >
            <div slot="error">
              <i class="el-icon-document" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="界面编码" align="center" prop="code" />
      <el-table-column label="活动参数" align="center" prop="arg" />
      <el-table-column label="跳转类型" align="center" prop="isReturn" >
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.isReturn == 0">不跳转</el-tag>
          <el-tag type="success" v-if="scope.row.isReturn == 1">APP</el-tag>
          <el-tag type="success" v-if="scope.row.isReturn == 2">HTML</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />

      <el-table-column label="是否开启" align="center" prop="isActivity" >

        <template slot-scope="scope">

          <el-tag type="warning" v-if="scope.row.isActivity == 0">未开启</el-tag>
          <el-tag type="success" v-if="scope.row.isActivity == 1">开启</el-tag>
          <el-tag type="success" v-if="scope.row.isActivity == 2">分时开启</el-tag>
        </template>
      </el-table-column>
      <!--<el-table-column label="开始时间" align="center" prop="beginTime" width="180">

      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">

      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sysActivity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sysActivity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动标题" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="活动关键词" prop="activityKey">
          <el-input v-model="form.activityKey" placeholder="请输入活动关键词" />
        </el-form-item>
        <el-form-item label="活动图片地址" prop="url">
          <el-upload
            :action="commonUploadApi"
            :accept="'.png,.jpg'"
            :file-list="fileList"
            :headers="headers"
            :before-remove="handleBeforeRemove"
            :on-success="handleSuccess"
            :on-error="handleError"
            :limit="1"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>


        <el-form-item label="跳转类型" prop="isReturn">
          <el-radio-group v-model="form.isReturn">
            <el-radio label="0">不跳转</el-radio>
            <el-radio label="1">跳转APP</el-radio>
            <el-radio label="2">跳转HTML</el-radio>
          </el-radio-group>
          <!--<el-input v-model="form.isReturn" placeholder="请输入跳转类型" />-->
        </el-form-item>
        <template v-if="form.isReturn==='1'">
          <el-form-item label="界面编码" prop="code">
            <el-input v-model="form.code" placeholder="请输入界面编码" />
          </el-form-item>
        </template>
        <el-form-item label="活动参数" prop="arg">
          <el-input v-model="form.arg" placeholder="请输入活动参数" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="礼品ID">
          <el-select
            v-model="form.activityGiftId"
            multiple
            placeholder="请选择礼品ID"
            clearable
            size="small"
          >

            <el-option
              v-for="gift in giftList"
              :key="gift.giftId"
              :label="gift.giftName"
              :value="gift.giftId"
            />


          </el-select>
        </el-form-item>
        <el-form-item label="是否开启" prop="isActivity">
         <!-- <el-input v-model="form.isActivity" placeholder="请输入是否开启" />-->
          <el-radio-group v-model="form.isActivity">
            <el-radio label='0' >不开启</el-radio>
            <el-radio label='1' >开启</el-radio>
            <el-radio label='2' >分时段开启</el-radio>
          </el-radio-group>
        </el-form-item>
        <template v-if="form.isActivity === '2'">
          <el-form-item label="开始时间" prop="beginTime">
            <el-time-picker clearable size="small" style="width: 200px"
                            v-model="form.beginTime"
                            type="time"
                            value-format="HH:mm:ss"
                            placeholder="选择开始时间">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-time-picker clearable size="small" style="width: 200px"
                            v-model="form.endTime"
                            type="time"
                            value-format="HH:mm:ss"
                            placeholder="选择结束时间">
            </el-time-picker>
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>
<style>
  .image{
    width: 75px;
    height: 60px;
  }
</style>
<script>
import { listSysActivity, getSysActivity, delSysActivity, addSysActivity, updateSysActivity, exportSysActivity,listGoodsGift } from "@/api/system/sysActivity";
import {mapGetters} from "vuex";
import { getToken } from "@/utils/auth";

export default {
  name: "SysActivity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 系统活动表格数据
      sysActivityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        activityKey: undefined,
        url: undefined,
        code: undefined,
        arg: undefined,
        isReturn: undefined,
        sort: undefined,
        ordersType: undefined,
        isActivity: undefined,
        beginTime: undefined,
        endTime: undefined
      },
      giftList: [

      ],
      // 表单参数
      form: {},
      headers: { Authorization: "Bearer " + getToken() },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "活动标题不能为空", trigger: "blur" }
        ],
        activityKey: [
          { required: true, message: "活动关键词不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "界面编码不能为空", trigger: "blur" }
        ],
        isReturn: [
          { required: true, message: "跳转类型不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        ordersType: [
          { required: true, message: "订单类型（10=普通订单，20=免费送,30=拼团订单,40=商家补贴,50=捶一捶,60=好友助力)不能为空", trigger: "blur" }
        ],
        isActivity: [
          { required: true, message: "是否开启（0=不开启，1=开启，2=分时段开启）不能为空", trigger: "blur" }
        ],
      }
    };
  },
  computed: {
    ...mapGetters([
      'commonUploadApi'
    ])
  },
  created() {
    this.getList();
    listGoodsGift().then(response =>{
      this.giftList=response.data;
    });
  },
  methods: {
    /** 查询系统活动列表 */
    getList() {
      this.loading = true;
      listSysActivity(this.queryParams).then(response => {
        this.sysActivityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        activityKey: undefined,
        url: undefined,
        code: undefined,
        arg: undefined,
        isReturn: undefined,
        sort: undefined,
        ordersType: undefined,
        isActivity: undefined,
        beginTime: undefined,
        endTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    handleSuccess(response, file, fileList){
      this.form.url = response.url;
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message);
      this.msgError(msg.message);
    },
    handleBeforeRemove(file, fileList) {
      this.form.url = null;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSysActivity(id).then(response => {
        this.form = response.data;
        this.form.isActivity=response.data.isActivity+'';
        if (this.form.activityGiftId != null) {
          this.form.activityGiftId = this.form.activityGiftId.split(",");
        }
        this.fileList = [{url:response.data.url}];
        this.open = true;
        this.title = "修改系统活动";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          this.form.activityGiftId =
            this.form.activityGiftId instanceof Array
              ? this.form.activityGiftId.join(",")
              : this.form.activityGiftId;

          if (this.form.id != undefined) {
            updateSysActivity(this.form).then(response => {
               this.msgSuccess("修改成功");
               this.open = false;
               this.getList();
            });
          } else {
            addSysActivity(this.form).then(response => {
               this.msgSuccess("新增成功");
               this.open = false;
               this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除系统活动编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSysActivity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有系统活动数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSysActivity(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  },

};





</script>
